package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.GoodsDao;
import com.hs.panghu.dao.ShopDao;
import com.hs.panghu.dao.impl.GoodsDaoImpl;
import com.hs.panghu.dao.impl.ShopDaoImpl;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        GoodsDao goodsDao=new GoodsDaoImpl();
        if ("querySearchShopGoodsList".equals(action)){
            String goodsName = req.getParameter("goodsName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String goodsStatus = req.getParameter("goodsStatus");
            String shopName = req.getParameter("shopName");

            Map<String,String>map=new HashMap<>();
            map.put("keyword",shopName);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("goodsStatus",goodsStatus);
            map.put("goodsName",goodsName);

            List<Goods> list = goodsDao.selectGoods(map);
            System.out.println(list);
            int count = goodsDao.selectGoodsCount(map);

            Map map1=new HashMap();
            map1.put("list",list);
            map1.put("total",count);

            Result success = Result.success(map1);

            resp.getWriter().write(JSON.toJSONString(success));
        }else if ("modifyShopGoods".equals(action)){
            String goodsId = req.getParameter("goodsId");
            int row = goodsDao.modifyShopGoods(Integer.parseInt(goodsId));
            if (row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
